﻿package {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	public class game extends MovieClip {
		var rabbit:Rabbit=new Rabbit();
		var step:Number=1;
		var gameState:int=1;//1 menu,2 开场,3游戏
		var radish:int=0;
		var biotic:int=3;
		var radishScoreBoard:number=new number();
		var bioticScoreBoard:number=new number();
		var radishIco:萝卜=new 萝卜();
		var bioticIco:头像=new 头像();
		var curtain:幕布MC=new 幕布MC();
		
		var couldLayer:Sprite=new Sprite();
		var mountainLayer:Sprite=new Sprite();
		var backLayer:Sprite=new Sprite();
		var roadLayer:Sprite=new Sprite();
		var rabbitLayer:Sprite=new Sprite();
		var foreLayer:Sprite=new Sprite();
		var asterLayer:Sprite=new Sprite();
		var informationLayer:Sprite=new Sprite();
		var ground:Ground=new Ground(couldLayer,mountainLayer,backLayer,roadLayer,rabbitLayer,foreLayer,asterLayer);
		var time:Timer=new Timer(40,0);

		public function game():void {
			stage.addChild(couldLayer);
			stage.addChild(mountainLayer);
			stage.addChild(backLayer);
			stage.addChild(roadLayer);
			stage.addChild(rabbitLayer);
			stage.addChild(foreLayer);
			stage.addChild(asterLayer);
			stage.addChild(informationLayer);
			informationLayer.addChild(radishScoreBoard);
			informationLayer.addChild(bioticScoreBoard);
			radishIco.visible=false;
			bioticIco.visible=false;
			curtain.visible=false;
			curtain.gotoAndStop(1);
			curtain.x=0;
			curtain.y=0;
			informationLayer.addChild(radishIco);
			informationLayer.addChild(bioticIco);
			informationLayer.addChild(curtain);
			rabbitLayer.addChild(rabbit);
			init();
		}
		private function init():void {
			radish=0;
			biotic=3;
			gameState=0;
			gameStart();
		}
		private function gameStart():void {
			time.addEventListener(TimerEvent.TIMER,gamePlay);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,clickLogo);
			stage.addEventListener(Event.ENTER_FRAME,gameControl);
		}
		private function gameControl(event:Event):void {
			switch (gameState) {
				case 0 ://logo按任意键继续
					if (currentFrame==15) {
						this.gotoAndPlay(10);
					}
					break;
				case 1 :
					curtain.visible=true;
					curtain.play();
					stage.removeEventListener(MouseEvent.MOUSE_DOWN,clickLogo);
					rabbit.setState(0);
					gameState=2;
					break;
				case 2 :
					this.stop();
					if (curtain.currentFrame==10){
						gameState=3;
						gotoAndStop(16);
					}
					break;
				case 3 ://开场
					ground.Init(rabbit);

					radishIco.x=200;
					radishIco.y=100;
					bioticIco.x=200;
					bioticIco.y=150;
					radishIco.visible=true;
					bioticIco.visible=true;
					radishIco.scaleX=1.5;
					radishIco.scaleY=1.5;
					bioticIco.scaleX=1;
					bioticIco.scaleY=1;
					radishScoreBoard.x=250;
					radishScoreBoard.y=100;
					bioticScoreBoard.x=250;
					bioticScoreBoard.y=150;
					radishScoreBoard.setScale(1);
					bioticScoreBoard.setScale(1);
					radishScoreBoard.setNumber(String(radish));
					bioticScoreBoard.setNumber(String(biotic));
					gameState=4;
					break;
				case 4 ://等待
					if (curtain.currentFrame==30){
						gameState=5;
						curtain.visible=false;
						curtain.gotoAndStop(1);
					}
					rabbit.everyFrame();
					ground.rabbitBump();
					break;
				case 5 :
					radishIco.scaleX=1;
					radishIco.scaleY=1;
					bioticIco.scaleX=0.8;
					bioticIco.scaleY=0.8;
					radishScoreBoard.setScale(0.6);
					bioticScoreBoard.setScale(0.6);
					gameState=6;
					break;
				case 6 ://动画 
					radishScoreBoard.x+=(50-radishScoreBoard.x)/10;
					radishScoreBoard.y+=(20-radishScoreBoard.y)/10;
					bioticScoreBoard.x+=(50-bioticScoreBoard.x)/10;
					bioticScoreBoard.y+=(50-bioticScoreBoard.y)/10;
					radishIco.x+=(40-radishIco.x)/10;
					radishIco.y+=(20-radishIco.y)/10;
					bioticIco.x+=(40-bioticIco.x)/10;
					bioticIco.y+=(50-bioticIco.y)/10;
					rabbit.everyFrame();
					ground.rabbitBump();
					if (Math.floor(radishScoreBoard.x)==50) {
						gameState=7;
					}
					break;
				case 7 ://游戏 
					stage.removeEventListener(Event.ENTER_FRAME,gameControl);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseAct);
					stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseAct);
					time.start();
					rabbit.setState(1);
					break;
				case 8://重新开始
					time.stop();
					stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseAct);
					stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseAct);
					gameState=1;
					break;
				case 9 ://按任意键重新开始
					break;

			}
		}
		private function clickLogo(event:MouseEvent):void {
			gameState=1;
		}
		private function mouseAct(event:MouseEvent):void {
			step=int((event.stageX-rabbit.x-rabbitLayer.x)/40)+3;
			step=step<2?2:step;
			if (rabbit.state<4) {
				if (event.buttonDown) {
					rabbit.setState(4);
				} else {
					rabbit.setState(step>6?3:int(step/2));
				}
			} else {
				rabbit.setRunState(step>6?3:int(step/2));
			}
		}
		private function gamePlay(event:TimerEvent):void {
			if (rabbit.state>0 && rabbit.state<6) {
				rabbit.x+=step;
				ground.moveCamera(step,0);
				rabbit.everyFrame();
				radish+=ground.rabbitBump();
				radishScoreBoard.setNumber(String(radish));
				bioticScoreBoard.setNumber(String(biotic));
				if (rabbit.y>150) {
					if (biotic-->0) {
						gameState=8;
						rabbit.setState(0);
						gameStart();
					} else {
						stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseAct);
						stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseAct);
						rabbit.setState(8);
						ground.remove();
						gotoAndStop(17);
						
						radishIco.x=250;
						radishIco.y=150;
						bioticIco.x=250;
						bioticIco.y=200;
						radishIco.scaleX=1.5;
						radishIco.scaleY=1.5;
						bioticIco.scaleX=1;
						bioticIco.scaleY=1;
						radishScoreBoard.x=280;
						radishScoreBoard.y=150;
						bioticScoreBoard.x=280;
						bioticScoreBoard.y=200;
						radishScoreBoard.setScale(1);
						bioticScoreBoard.setScale(1);
						init();
					}
				}
			}else if(rabbit.state==8){
				rabbit.x=270;
				rabbit.y=0;
				rabbit.floor();
				rabbit.everyFrame();
			}
				
		}
	}
}